package com.yunquan.ohana.utils

import android.util.Log
import com.yunquan.ohana.BuildConfig

/**
 * 日志$
 *
 * @author yjp
 * @date 2021-06-11 16:54
 */
object L {
    //类名
    private var mClassName: String? = null

    //方法名
    private var mMethodName: String? = null

    //行数
    private var mLineNumber: Int = 0

    private fun isDebuggable(): Boolean {
        return true
//        return BuildConfig.DEBUG
    }

    private fun createLog(log: String): String {
        val sb = StringBuffer()
        sb.append("\r\n==============================\r\n")
            .append(mMethodName)
            .append("(")
            .append(mClassName)
            .append(":")
            .append(mLineNumber)
            .append(")")
            .append("\r\n==============================\r\n")
            .append(log)
        return sb.toString()
    }

    private fun getMethodNames(sElements: Array<StackTraceElement>) {
        val element = sElements[1]
        mClassName = element.fileName
        mMethodName = element.methodName
        mLineNumber = element.lineNumber
    }

    fun e(msg: String) {
        if (!isDebuggable()) return;
        getMethodNames(Throwable().stackTrace)
        Log.e(mClassName, createLog(msg))
    }

    fun e(msg: String, tr: Throwable) {
        if (!isDebuggable()) return;
        getMethodNames(Throwable().stackTrace)
        Log.e(mClassName, createLog(msg),tr)
    }

    fun i(msg: String) {
        if (!isDebuggable()) return;
        getMethodNames(Throwable().stackTrace)
        Log.i(mClassName, createLog(msg))
    }

    fun d(msg: String) {
        if (!isDebuggable()) return;
        getMethodNames(Throwable().stackTrace)
        Log.d(mClassName, createLog(msg))
    }

    fun v(msg: String) {
        if (!isDebuggable()) return;
        getMethodNames(Throwable().stackTrace)
        Log.v(mClassName, createLog(msg))
    }

    fun w(msg: String) {
        if (!isDebuggable()) return;
        getMethodNames(Throwable().stackTrace)
        Log.w(mClassName, createLog(msg))
    }
}